﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.NHibernateHelper;
using Domain.Entity;
using DataAccess.AbstractRepository;
using NHibernate;

namespace DataAccess.Repository
{
    public class JobBookingRepository : NHibRepository<JobBooking>, IJobBookingRepository
    {
        public JobBookingRepository(NHibContext context)
            : base(context)
        {
        }

        public virtual IList<JobBooking> GetAllJobBooking(JobBooking jobBooking, long customerId, long staffId, DateTime? from, DateTime? to)
        {
            string searchCriteria = "";
            if (customerId > 0)
            {
                searchCriteria = " jb.Customer.ID=" + customerId;
            }
            if (staffId > 0)
            {
                if (searchCriteria != string.Empty)
                {
                    searchCriteria += " and ";
                }
                searchCriteria += " jb.Staff.ID=" + staffId;
            }
            if (from.HasValue)
            {
                if (searchCriteria != string.Empty)
                {
                    searchCriteria += " and ";
                }
                searchCriteria += " jb.JobDateTime >='" + from + "'";
            }

            if (to.HasValue)
            {
                if (searchCriteria != string.Empty)
                {
                    searchCriteria += " and ";
                }
                searchCriteria += " jb.JobDateTime <='" + to + "'";
            }
            if (jobBooking.Status.ID > 0)
            {
                if (searchCriteria != string.Empty)
                {
                    searchCriteria += " and ";
                }
                searchCriteria += " jb.Status.ID=" + jobBooking.Status.ID;
            }
            if (jobBooking.BookingType.ID > 0)
            {
                if (searchCriteria != string.Empty)
                {
                    searchCriteria += " and ";
                }
                searchCriteria += " jb.BookingType.ID=" + jobBooking.BookingType.ID;
            }

            if (searchCriteria != string.Empty)
            {
                searchCriteria += " and ";
            }
            searchCriteria += " jb.IsDeleted=" + jobBooking.IsDeleted;

            string hsql = "from JobBooking jb where " + searchCriteria;
            IQuery query = Session.CreateQuery(hsql);
            return query.List<JobBooking>();
        }

        public virtual IList<JobBooking> GetJobBookingByInvoiceId(long invoiceId)
        {
            string hsql = "from JobBooking where InvoiceID = " + invoiceId;
            IQuery query = Session.CreateQuery(hsql);
            return query.List<JobBooking>();
        }
    }
}
